package so.go4.controller;

import com.alibaba.fastjson.JSON;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import so.go4.model.Article;
import so.go4.service.IArticleService;

import java.util.List;

/**
 * Created by SQB on 2015/5/23.
 */
@Controller
@RequestMapping("/article")
public class ArticleController {

    private static Logger logger = Logger.getLogger(ArticleController.class);

    @Autowired
    IArticleService articleService;

    @RequestMapping(value = "/{categories}/list",method = RequestMethod.GET)
    public ModelAndView listall(@PathVariable(value = "categories")String categories,@RequestParam(value = "pageNO",defaultValue = "1")int pageNO,@RequestParam(value = "pageSize",defaultValue = "5")int pageSize){
//        List<Article> article = articleService.selectAllContentsWithCate("");
        logger.info("pageSizeNO"+pageNO+pageSize);
        Article aa = new Article();
        aa.setCategories(categories);
        aa.setPageNO((pageNO-1)*pageSize);
        aa.setPageSize(pageSize);
        List<Article> article = articleService.testPojo(aa);
        int num = articleService.countArticles(aa);
        System.out.println(num+"----_----num");
        ModelAndView mav = new ModelAndView("list");
        logger.info(JSON.toJSONString(article));
        mav.addObject("article",article);
        mav.addObject("num",num);
        mav.addObject("pageSize",pageSize);
        mav.addObject("pageNO",pageNO);
        mav.addObject("categories",categories);
        return mav;
    }

//    @RequestMapping("/java/list")
//    public ModelAndView listJava(){
////        List<Article> article =articleService.selectAllContentsWithCate("java");
//        Article aa = new Article();
//        aa.setCategories("java");
//        List<Article> article = articleService.testPojo(aa);
//        ModelAndView mav = new ModelAndView("list");
//        logger.info(JSON.toJSONString(article));
//        return mav.addObject("article",article);
//    }

//    @RequestMapping("/linux/list")
//    public ModelAndView listLinux(){
////        List<Article> article =articleService.selectAllContentsWithCate("linux");
//        Article aa = new Article();
//        aa.setCategories("linux");
//        List<Article> article = articleService.testPojo(aa);
//        ModelAndView mav = new ModelAndView("list");
//        logger.info(JSON.toJSONString(article));
//        return mav.addObject("article",article);
//    }

    @RequestMapping(value="/detail/{id}",method={RequestMethod.GET})
    public ModelAndView articleDetail(@PathVariable(value = "id") Integer id){
        Article article = articleService.getArticleById(id);
        ModelAndView mav = new ModelAndView("detail");
        logger.info(JSON.toJSONString(article));
        return mav.addObject("article",article);
    }

   /* @RequestMapping("/list2")
    public ModelAndView listall2(){
        Article article = articleService.getArticleById(1);
        ModelAndView mav = new ModelAndView("redirect:/list.jsp");
        logger.info(JSON.toJSONString(article));
        return mav.addObject("article",article);
    }

    @RequestMapping("/list1")
    public String listall1(HttpServletRequest request){
        Article article = articleService.getArticleById(1);
        logger.info(JSON.toJSONString(article));
        request.setAttribute("article",article);
        return "list";
    }*/
}
